package com.zc.common.mq.utils;

import org.apache.commons.lang3.StringUtils;
import org.apache.rocketmq.common.message.Message;

import java.nio.charset.StandardCharsets;

/**
 * <p>Description : </p>
 *
 * @author : xuhongcao
 * @version : v1.0.0
 * @since : 2/4/2022
 **/
public class MessageUtil {

    public MessageUtil() {
    }

    public static Message getMessage(String topic, String messageKey, String tag, String messageStr, int delayLevel) {
        Message rocketMqMessage = new Message();
        rocketMqMessage.setTopic(topic);
        rocketMqMessage.setKeys(messageKey);
        rocketMqMessage.setBody(messageStr.getBytes(StandardCharsets.UTF_8));
        if (StringUtils.isNotBlank(tag)) {
            rocketMqMessage.setTags(tag);
            rocketMqMessage.putUserProperty("SQL_TAG", tag);
        }

        if (delayLevel > 0) {
            rocketMqMessage.setDelayTimeLevel(delayLevel);
        }

        return rocketMqMessage;
    }
}
